var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    activity_list:{
      type:Array
    },
    type:{
      type:String,
      value:""
    },
    width:{
      type:Number
    }
  },
  observers:{
    activity_list:function(arr){
      if(arr && arr.length!==0){
        this.setData({
          activity:arr,
        })
        this.getData();
        this.show()
      }

    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    show:false,
    activity:[],
    list: [
      { title: "认购活动", value: 0 },
      { title: "待发放", value: 3 },
      { title: "认购失败", value: -1 },
      { title: "结算重开", value: 6 },
      { title: "已完成", value: 5 }
    ],
    active: 0,
    activedata: 0,
    currentIndex:0,
    scroll_left:0,
    left_clix:0,
    timeId:null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    show(){
      if(this.data.activity_list){
        console.log(this.data.activity_list)
      }
      console.log(this.data.activity_list,"activity_list======");
      if(this.data.timeId){
        this.setData({timeId:null});
      }
      this.data.timeId = setTimeout(()=>{
        this.setLine(0);
      },500)
    },
    onChange(e){
      console.log(e)
      this.setData({
        activedata:e.detail.name
      })
      this.getData()
    },
    handle(e){

      let offsetLeft = e.currentTarget.offsetLeft;
      if(offsetLeft>this.data.width/2){
          offsetLeft = offsetLeft - 175;
          this.setData({scroll_left:offsetLeft})
      }else if(offsetLeft<this.data.width/2){
        this.setData({scroll_left:0})
      }
      this.setData({
        currentIndex:e.currentTarget.dataset.index,
        activedata:e.currentTarget.dataset.id
      })
      this.getData()
      this.setLine(e.currentTarget.dataset.index)

      var query = wx.createSelectorQuery();
      query.select(e).boundingClientRect();
      query.selectViewport().scrollOffset();
      query.exec((res)=>{
        console.log(res)
      })
    },
    getData(){
      let url = '';
      if(this.data.type == 'home'){
        url ="plugin.subscription.frontend.modules.activity.controllers.detail.index"
      }else{
        let arr =  this.data.list.filter(item => item.value !==1);
        this.setData({list:arr})
        url = "plugin.subscription.frontend.modules.member.controllers.stage.index";
      }
      let json = {
        stage_status:this.data.active
      }
      if(this.data.activedata == 0){
        json.activity_id = this.data.activity[0].id
      }else{
        json.activity_id = this.data.activedata
      }
      app._postNetWork({
        url:app.getNetAddresss(url),
        data:json,
        success:res=>{
          if(res.data.result == 0){
            app.tips(res.data.msg);
          }
          let activity_id = 0;
          if(this.data.activedata == 0){
            activity_id = this.data.activity[0].id
          }else{
            activity_id = this.data.activedata
          }
          this.triggerEvent('activeInfo',{data:res.data.data,active:this.data.active,activity_id,stage_status:this.data.active})
        }
      })
    },
    change({ currentTarget: { dataset: { i } } }) {
      if(this.data.active == i) return false;
      this.setData({
        active: i
      })
      this.getData()
    },
    setLine(currentIndex){
      Promise.all([
        this.getAllRect('.box-item'),
        this.getRect('.current_line')])
      .then(([rects=[],lineRect])=>{
        const rect = rects[currentIndex];
        if(rect == null){
          return;
        }
        let lineOffsetLeft = rects
          .slice(0,currentIndex)
          .reduce((prev,curr)=>prev+curr.width,0);
        lineOffsetLeft+=(rect.width-lineRect.width)/2;
        this.setData({
          left_clix:lineOffsetLeft
        })
      }).catch(err => {
        console.log("err=>",err)
      })
    },
    //获取所有滚动内的元素
     getAllRect(selector) {
      return new Promise((resolve) => {
        wx.createSelectorQuery()
          .in(this)
          .selectAll(selector)
          .boundingClientRect()
          .exec((rect = []) => resolve(rect[0]));
      });
    },
    //获取单个元素的信息
    getRect(selector) {
      return new Promise((resolve) => {
        wx.createSelectorQuery()
          .in(this)
          .select(selector)
          .boundingClientRect()
          .exec((rect = []) => resolve(rect[0]));
      });
    }
  },
  lifetimes:{
    attached() {
      const query = wx.createSelectorQuery();
      query.select('.status-box').boundingClientRect(function (res) {
        console.log(res, "status-box");
      })
      query.exec();
      this.setData({
        show:true
      })
    }
  }

})
